class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> ans = new ArrayList<String>();

        if(nums.length == 0) return ans;

        if(nums.length == 1){
            ans.add("" + nums[0]);
            return ans;
        }

        int pre = nums[0], left = nums[0];
        for(int i = 1; i < nums.length; i++) {

            if((nums[i] - pre) == 1) {
                pre = nums[i];  //更新遍历到当前的前一个
                continue;
            }

            if(left == pre) {
                ans.add("" + left);
            }else {
                ans.add("" + left + "->" + pre);
            }

            //更新
            pre = nums[i];
            left = nums[i];
        }

        if(left == pre) {
            ans.add("" + left);
        }else {
            ans.add("" + left + "->" + pre);
        }

        return ans;
    }
}